package com.wc.alorithm_blue_bridge._动态规划.线性DP.跳跃;

import java.util.Arrays;
import java.util.Scanner;

/**
 * @Author congge
 * @Date 2023/10/9 23:26
 * @description
 * 跳跃
 * https://www.lanqiao.cn/problems/553/learning/?page=1&first_category_id=1&sort=students_count&second_category_id=3
 */
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int m = sc.nextInt();
        int n = sc.nextInt();

        int[][] dp = new int[m][n];
        int[][] array = new int[m][n];

        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                array[i][j] = sc.nextInt();
            }
        }

        /**
         * 填入最小值
         */
        for (int i = 0; i < m; i++) {
            Arrays.fill(dp[i], -10000);
        }

        /**
         * 初始化边界
         */
        dp[0][0] = array[0][0];
        for (int i = 1; i < n; i++) {
            int max = -10000;
            for (int j = Math.max(0, i - 3); j < i; j++) {
                max = Math.max(max, dp[0][j]);
            }
            dp[0][i] = max + array[0][i];
        }

        for (int i = 1; i < m; i++) {
            for (int j = 0; j < n; j++) {
                /**
                 * 遍历小方块,取出方块内的最大值
                 */
                int max = -10000;
                int left = Math.max(0, j - 3);
                int right = j;
                int up = Math.max(0, i - 3);
                int down = i;

                for (int k = up; k <= down; k++) {
                    for (int l = left; l <= right; l++) {
                        if (k == down && l == right) {
                            break;
                        }
                        max = Math.max(dp[k][l], max);
                    }
                }
                dp[i][j] = max + array[i][j];
            }
        }

        System.out.println(dp[m - 1][n - 1]);
    }
}
